﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI.Michsky.UI.ModernUIPack;

public class BgColorAnim : MonoBehaviour
{
    public UnityEvent ChangeEvent = new UnityEvent();
    private UIGradient uIGradient;

    Gradient gradient = new Gradient();

    private void Awake()
    {
        ChangeEvent.AddListener(ChangeBgColor);
        uIGradient = GetComponent<UIGradient>();
    }

    private void ChangeBgColor()
    {
        StartCoroutine("AlphaCor");
    }

    IEnumerator AlphaCor()
    {
        Color color = new Color(Random.Range(0, .99f), Random.Range(0, .99f), Random.Range(0, .99f));
        Color color_2 = new Color(Random.Range(0, .99f), Random.Range(0, .99f), Random.Range(0, .99f));
        GradientColorKey[] colors = new GradientColorKey[] { new GradientColorKey(color, .0f), new GradientColorKey(color_2, 1.0f) };


        float alpha = .0f;
        while(alpha < 1)
        {
            alpha += Time.deltaTime;
            alpha = Mathf.Clamp01(alpha);
            gradient.SetKeys(colors,
            new GradientAlphaKey[] { new GradientAlphaKey(alpha, .0f), new GradientAlphaKey(alpha, 1.0f) });
            uIGradient.EffectGradient = gradient;
            
            yield return null;
        }
    }
}
